package cn.vcjf.weapp.api.goods.controller;


import cn.vcjf.weapp.api.goods.entity.GoodsPropertyValue;
import cn.vcjf.weapp.api.goods.service.GoodsPropertyValueService;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.ResponseBody;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.vcjf.weapp.api.goods.entity.GoodsProperty;
import cn.vcjf.weapp.api.goods.service.GoodsPropertyService;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Controller
@RequestMapping("/goods/goodsProperty")
public class GoodsPropertyController {
	@Resource
	private GoodsPropertyService goodsPropertyService;
	@Resource
	private GoodsPropertyValueService goodsPropertyValueService;

	/**
	 * 商品规格(根据商品的类型获取商品的规格)
	 * @param goodsProperty
	 * @return
	 */
	@RequestMapping("/findGoodsSpecificationList")
	@ResponseBody
	public TableResult findGoodsSpecificationList(GoodsProperty goodsProperty) {

		EntityWrapper<GoodsProperty> wrapper = new EntityWrapper<GoodsProperty>();

		wrapper.eq("sysUserId", goodsProperty.getSysUserId());
		wrapper.eq("goodsTypeId", goodsProperty.getGoodsTypeId());

		Page<GoodsProperty> page = goodsProperty.getPageInfo();

		page = goodsPropertyService.selectPage(page, wrapper);
		for(GoodsProperty property:page.getRecords()) {
			Wrapper<GoodsPropertyValue> query = new EntityWrapper<GoodsPropertyValue>();
			query.eq("proTypeId", property.getId());
			List<GoodsPropertyValue> goodsProperytValues = goodsPropertyValueService.selectList(query);
			property.setGoodsPropertyValueList(goodsProperytValues);

		}
		return TableResult.OKResult(page);

	}

	@RequestMapping("/findGoodsPropertyOne")
	@ResponseBody
	public TableResult findGoodsPropertyOne(GoodsProperty goodsProperty) {

		EntityWrapper<GoodsProperty> wrapper = new EntityWrapper<GoodsProperty>();

		if (goodsProperty.getId() != null) {
			wrapper.eq("id", goodsProperty.getId());
		}
		//Todo

		GoodsProperty result = goodsPropertyService.selectOne(wrapper);

		return TableResult.OKResult(result);
	}

	@RequestMapping("/findGoodsPropertyList")
	@ResponseBody
	public TableResult findGoodsPropertyList(GoodsProperty goodsProperty) {

		EntityWrapper<GoodsProperty> wrapper = new EntityWrapper<GoodsProperty>();

		if (goodsProperty.getId() != null) {
			wrapper.eq("id", goodsProperty.getId());
		}
		//Todo

		Page<GoodsProperty> page = goodsProperty.getPageInfo();

		page = goodsPropertyService.selectPage(page, wrapper);

		return TableResult.OKResult(page);

	}

	@RequestMapping("/deleteGoodsProperty")
	@ResponseBody
	public TableResult deleteGoodsProperty(GoodsProperty goodsProperty) {
		goodsProperty.preUpdate();
		boolean b = goodsPropertyService.deleteById(goodsProperty.getId());
		return TableResult.Result(b);
	}

}
